#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int count = 0;//歌曲库中的歌曲数
int count1 = 0;//点歌的数量
struct song
{
	int num;
	char song_name[20];
	char man_name[10];
	struct song* next;
};
struct song* Initlinklist(); //初始化链表
int menu();//主菜单
int menu_first();//二级菜单-1
int menu_second();//二级菜单-2
struct song* songname_sort(struct song*);//按照歌曲名字排序
struct song* manname_sort(struct song*);//按照歌手名字排序
struct song* option(struct song*, struct song*);//点歌
void pass(struct song*);//遍历
struct song* top(struct song*, int);//置顶函数
struct song* ahead(struct song*, int);//上移一位
int main()
{
	struct song* head, * following, * s, * head1 = NULL;
	head = NULL;
	int i = 4;
	//初始化曲库
	head = Initlinklist();
	//菜单
	while (1)
	{
		int opt = menu();
		if (opt == 1)
		{
			//查看目前的歌曲库
			// 添加新曲库
			//遍历
			following = head;
			char song_name[20];
			char man_name[10];
			int num = 0;
			printf("序号\t歌曲名称\t歌手\n");
			while (following->next != NULL)
			{
				printf("%d\t%-8s\t%s\n", following->num, following->song_name, following->man_name);
				following = following->next;
			}
			printf("%d\t%-8s\t%s\n", following->num, following->song_name, following->man_name);
			printf("是否手动添加歌曲进入歌曲库（Yes(y)/NO(n)）：\n");
			char Isright = ' ';
			while (1)
			{
				getchar();
				Isright = getchar();
				if (Isright == 'y')
				{
					printf("请添加歌曲名称、歌手\n");
					s = (struct song*)malloc(sizeof(struct song));
					num = i;
					scanf("%s", song_name);
					scanf("%s", man_name);
					s->num = num;
					strcpy(s->song_name, song_name);
					strcpy(s->man_name, man_name);
					i++;
					count++;
					s->next = following->next;
					following->next = s;
				}
				else
				{
					system("pause");
					system("cls");
					break;
				}
				printf("是否继续手动添加歌曲进入歌曲库（Yes(y)/NO(n)）：\n");
			}
		}
		else if (opt == 2)
		{
			//点歌，二级菜单
			int flag = 1;
			while (1)
			{
				if (flag == 0)
					break;
				int ret1 = menu_first();
				following = head;
				if (ret1 == 1)
				{
					//按照歌曲库歌曲名字排序
					songname_sort(head);
					//遍历
					/*while (following->next != NULL)
					{
						printf("%d\t%-8s\t%s\n", following->num, following->song_name, following->man_name);
						following = following->next;
					}
					printf("%d\t%-8s\t%s\n", following->num, following->song_name, following->man_name);*/
					pass(head);
					flag = 0;
					head1 = option(head, head1);
					system("pause");
					system("cls");
				}
				else if (ret1 == 2)
				{
					//按照演唱者的名字排序
					manname_sort(head);
					//遍历
					/*while (following->next != NULL)
					{
						printf("%d\t%-8s\t%s\n", following->num, following->song_name, following->man_name);
						following = following->next;
					}
					printf("%d\t%-8s\t%s\n", following->num, following->song_name, following->man_name);*/
					pass(head);
					flag = 0;
					head1 = option(head, head1);
					system("pause");
					system("cls");
				}
				else if (ret1 == 0)
				{
					//合理性判断
					flag = 0;
					system("pause");
					system("cls");
					break;
				}
				else
				{
					printf("您输入的选项有误，请重新输入\n");
					system("pause");
					system("cls");

				}
			}
		}
		else if (opt == 3)
		{
			//已选歌单，二级菜单
			while (1)
			{
				int ret2 = menu_second();
				if (ret2 == 1)
				{
					//显示已点歌单---遍历head1
					struct song* p;
					p = head1;
					int a = 1;
					if (p == NULL)
					{
						printf("还未有点播的歌曲\n");
						system("pause");
						system("cls");
						break;
					}
					printf("序号\t歌曲名称\t歌手\n");
					while (p->next != NULL)
					{
						p->num = a;
						printf("%d\t%-8s\t%s\n", p->num, p->song_name, p->man_name);
						p = p->next;
						a++;
					}
					p->num = a;
					printf("%d\t%-8s\t%s\n", p->num, p->song_name, p->man_name);
					system("pause");
					system("cls");
				}
				else if (ret2 == 2)
				{
					//查询
					struct song* p;
					p = head1;
					char arr[20];
					int flag = 0;
					if (p == NULL)
					{
						printf("还未有点播的歌曲\n");
						system("pause");
						system("cls");
						break;
					}
					printf("请输入要查询的歌曲名字(输入零退出)：");
					scanf("%s", arr);
					while (arr[0] != '0')
					{
						p = head1;
						while (p->next != NULL)
						{			
							flag = 0;
							if (strcmp(p->song_name, arr) == 0)
							{
								flag = 1;
								break;
							}
							p = p->next;
						}
						if (flag == 0 && strcmp(p->song_name, arr) == 0)
						{
							flag == 1;
						}
						if (flag == 1)
						{
							printf("序号\t歌曲名称\n");
							printf("%d\t%-8s\n", p->num, p->song_name);
						}
						else
						{
							printf("未点播此歌曲\n");
						}
						printf("请输入要查询的歌曲名字(输入零退出)：");
						scanf("%s", arr);
					}
					system("pause");
					system("cls");
				}
				else if (ret2 == 3)
				{
					//删除
					int b = 0, flag = 0;
					if (head1 == NULL)
					{
						printf("还未有点播的歌曲\n");
						system("pause");
						system("cls");
						break;
					}
					pass(head1);
					printf("请选择要删除歌曲的序号（输入小于等于零结束）：\n");
					scanf("%d", &b);
					while (b > 0)
					{
						struct song* p, * q;
						p = head1;//待删
						q = head1;//待删前一个
						while (p == head1)
						{
							//要删的是头节点
							if (p->num == b)
							{
								q = q->next;
								head1 = q;
								free(p);
								p = q;
								count1--;
							}
							else
								p = p->next;
						}
						while (p->next != NULL)
						{
							//删中间节点
							if (p->num == b)
							{

								q->next = p->next;
								free(p);
								p = q->next;
								count1--;
							}
							else
							{
								p = p->next;
								q = q->next;
							}
						}
						if (p->next == NULL)
						{
							//删尾节点
							if (p->num == b)
							{
								q->next = NULL;
								free(p);
								count1--;
							}
						}
						printf("请选择要删除歌曲的序号（输入小于等于零结束）：\n");
						scanf("%d", &b);
					}
					printf("\n");
					pass(head1);
					system("pause");
					system("cls");
				}
				else if (ret2 == 4)
				{
					//置顶
					struct song* p;//要置顶的歌曲
					int num;
					if (head1 == NULL)
					{
						printf("还未有点播的歌曲\n");
						system("pause");
						system("cls");
						break;
					}
					pass(head1);
					printf("请选择要置顶的歌曲序号（输入零结束）：");
					scanf("%d", &num);
					while (num != 0)
					{
						head1 = top(head1, num);
						printf("\n");
						pass(head1);
						system("pause");
						system("cls");
						printf("请选择要置顶的歌曲序号（输入零结束）：");
						scanf("%d", &num);
					}
				}
				else if (ret2 == 5)
				{
					//上移一位
					int num;
					if (head1 == NULL)
					{
						printf("还未有点播的歌曲\n");
						system("pause");
						system("cls");
						break;
					}
					pass(head1);
					printf("请输入要上移一位歌曲的序号\n");
					scanf("%d", &num);
					ahead(head1, num);
					pass(head1);
					system("pause");
					system("cls");
				}
				else if (ret2 == 0)
				{
					//退出
					system("pause");
					system("cls");
					break;
				}
				else
				{
					//合理性判断
					printf("选择错误，请重新选择\n");
					system("pause");
					system("cls");
				}
			}
		}
		else if (opt == 0)
		{
			break;
		}
		else
		{
			printf("您输入的选项有误，请重新输入：\n");
			system("pause");

			system("cls");
		}
	}
	return 0;
}
void pass(struct song* head)
{
	struct song* p;
	p = head;
	int i = 1;
	if (p == NULL)
	{
		printf("暂无点歌\n");
	}
	else
	{
		while (p->next != NULL)
		{
			p->num = i;
			printf("%d\t%-8s\t%s\n", p->num, p->song_name, p->man_name);
			p = p->next;
			i++;
		}
		p->num = i;
		printf("%d\t%-8s\t%s\n", p->num, p->song_name, p->man_name);
	}
}
int menu()
{
	printf("##################################\n");
	printf("#      1.查看目前的歌曲库^       #\n");
	printf("#      2.点歌^                   #\n");
	printf("#      3.已点歌曲^               #\n");
	printf("#      0.退出                    #\n");
	printf("##################################\n");
	printf("请输入选项：");
	int opt;
	scanf("%d", &opt);
	return opt;
}
struct song* Initlinklist()
{
	struct song* head, * s, * tail;
	head = NULL;
	s = (struct song*)malloc(sizeof(struct song));
	s->num = 1;
	strcpy(s->song_name, "左手右手");
	strcpy(s->man_name, "郭子荷");
	s->next = NULL;
	head = s;
	tail = s;
	s = s->next;
	s = (struct song*)malloc(sizeof(struct song));
	s->num = 2;
	strcpy(s->song_name, "红颜乱");
	strcpy(s->man_name, "刘涛");
	tail->next = s;
	tail = tail->next;
	s = (struct song*)malloc(sizeof(struct song));
	s->num = 3;
	strcpy(s->song_name, "凉凉");
	strcpy(s->man_name, "张碧晨");
	s->next = NULL;
	tail->next = s;
	tail = tail->next;
	count = 3;
	return head;
}
int menu_first()
{
	int opt = 0;
	printf("########################################\n");
	printf("#    1）按照歌曲库中歌曲拼音字母排序   #\n");
	printf("#    2）按照演唱者名字首拼音字母排序   #\n");
	printf("#    0）退出                           #\n");
	printf("########################################\n");
	printf("请选择选项\n");
	scanf("%d", &opt);
	return opt;
}
int menu_second()
{
	int opt = 0;
	printf("################################\n");
	printf("#    1）显示已点歌单           #\n");
	printf("#    2）查询歌曲               #\n");
	printf("#    3）删除歌曲               #\n");
	printf("#    4）置顶歌曲               #\n");
	printf("#    5）上移一位               #\n");
	printf("#    0）退出                   #\n");
	printf("################################\n");
	printf("请选择选项\n");
	scanf("%d", &opt);
	return opt;
}
struct song* songname_sort(struct song* head)
{
	struct song* p;
	p = head;
	int i = 0, flag = 0;
	char song_name[20];
	char man_name[20];
	for (i; i < count; i++)
	{
		p = head;
		while ((p->next)->next != NULL)
		{
			if (strcmp(p->song_name, (p->next)->song_name) == 1)
			{
				strcpy(song_name, p->song_name);
				strcpy(p->song_name, (p->next)->song_name);
				strcpy((p->next)->song_name, song_name);
				strcpy(man_name, p->man_name);
				strcpy(p->man_name, (p->next)->man_name);
				strcpy((p->next)->man_name, man_name);
			}
			p = p->next;
		}
		if (strcmp(p->song_name, (p->next)->song_name) == 1)
		{
			strcpy(song_name, p->song_name);
			strcpy(p->song_name, (p->next)->song_name);
			strcpy((p->next)->song_name, song_name);
			strcpy(man_name, p->man_name);
			strcpy(p->man_name, (p->next)->man_name);
			strcpy((p->next)->man_name, man_name);
		}
	}
	return head;
}
struct song* manname_sort(struct song* head)
{
	struct song* p;
	p = head;
	int i = 0, flag = 0;
	char song_name[20];
	char man_name[20];
	for (i; i < count; i++)
	{
		p = head;
		while ((p->next)->next != NULL)
		{
			if (strcmp(p->man_name, (p->next)->man_name) == 1)
			{
				strcpy(song_name, p->song_name);
				strcpy(p->song_name, (p->next)->song_name);
				strcpy((p->next)->song_name, song_name);
				strcpy(man_name, p->man_name);
				strcpy(p->man_name, (p->next)->man_name);
				strcpy((p->next)->man_name, man_name);
			}
			p = p->next;
		}
		if (strcmp(p->man_name, (p->next)->man_name) == 1)
		{
			strcpy(song_name, p->song_name);
			strcpy(p->song_name, (p->next)->song_name);
			strcpy((p->next)->song_name, song_name);
			strcpy(man_name, p->man_name);
			strcpy(p->man_name, (p->next)->man_name);
			strcpy((p->next)->man_name, man_name);
		}
	}
	return head;
}
struct song* option(struct song* head, struct song* head1)
{
	struct song* s, * p, * tail;
	s = head;
	p = head1;
	tail = head1;
	int num, i = 1, index = 0;
	char song_name[20];
	char man_name[10];
	printf("请输入想选的歌曲序号(输入零结束):");
	scanf("%d", &num);
	while (num != 0)
	{
		s = head;
		if (num > count || num < 0)
		{
			printf("查无此序号歌曲，请重新输入：");
			scanf("%d", &num);
			continue;
		}
		int a = 1;
		for (i = 1; i <= count; i++)
		{
			if (s->num == num)
			{
				p = (struct song*)malloc(sizeof(struct song));
				p->num = a;
				strcpy(p->song_name, s->song_name);
				strcpy(p->man_name, s->man_name);
				if (head1 == NULL)
				{
					head1 = p;
					p->next = NULL;
					tail = p;
				}
				else
				{
					p->next = tail->next;
					tail->next = p;
					tail = tail->next;
				}
			}
			s = s->next;
			a++;
		}
		index = num;
		count1++;
		printf("请输入想选的歌曲序号(输入零结束):");
		scanf("%d", &num);
	}
	return head1;
}
struct song* top(struct song* head, int num)
{
	int my_count = 1;
	struct song* p, * q, * head1;
	head1 = NULL;
	p = head;//待置顶
	q = head;//待置顶前一个
	int flag = 0;
	if (num > count1 || num < 0)
		printf("点的歌单中暂无此歌曲\n");
	else
	{
		while (p->next != NULL)
		{
			if (p == head && p->num == num)
			{
				printf("此歌已置顶，请重新输入：\n");
				break;
			}
			else if (p->num == num && p != head)
			{
				q->next = p->next;
				p->next = head;
				head1 = p;
				flag = 1;
				my_count++;
				break;
			}
			else if (p == head && p->num != num)
				p = p->next;
			else
			{
				p = p->next;
				q = q->next;
			}
		}
		if (p->num == num && p != head && my_count != 2)
		{
			q->next = p->next;
			p->next = head;
			head = p;
			flag = 1;
			head1 = p;
		}
		if (flag == 1)
			return head1;
	}
}
struct song* ahead(struct song* head, int num)
{
	int flag = 0;
	struct song* p, * q, * head1 = NULL;
	char songname[20];
	char manname[20];
	p = head;//待前移
	q = head;//待前移的前一个
	if (num > count1 || num < 0)
		printf("点的歌单中暂无此歌曲\n");
	else
	{
		while (p->next != NULL)
		{
			if (p == head && p->num == num)
			{
				printf("已在最前面，无需前移\n");
				break;
			}
			else if (p != head && p->num == num)

			{
				strcpy(songname, p->song_name);
				strcpy(p->song_name, q->song_name);
				strcpy(q->song_name, songname);
				strcpy(manname, p->man_name);
				strcpy(p->man_name, q->man_name);
				strcpy(q->man_name, manname);
				head1 = p;
				flag = 1;
				break;
			}
			else if (p == head && p->num != num)
			{
				p = p->next;
			}
			else
			{
				p = p->next;
				q = q->next;
			}
		}
		if (flag == 0 && p->next && p->num == num)
		{
			strcpy(songname, p->song_name);
			strcpy(p->song_name, q->song_name);
			strcpy(q->song_name, songname);
			strcpy(manname, p->man_name);
			strcpy(p->man_name, q->man_name);
			strcpy(q->man_name, manname);
			head1 = p;
			flag = 1;
		}
		if (flag == 1)
		{
			return head1;
		}
	}
}